﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Top.Api;
using Top.Api.Request;
using Top.Api.Response;
using Yunzuan.Foundation.Entities;
using Yunzuan.Foundation.Log;

namespace Yunzuan.Foundation.Web
{
    public static class SMSManager
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tel"></param>
        /// <param name="lib">SMSLib</param>
        /// <param name="param">各自短信内容替换的参数</param>
        /// <returns></returns>
        public static string SendSms(string tel, SMSLib lib, HashParam param)
        {
            var smsContent = "";
            var tempSmsCode = "";
            var smsSign = "珠宝系统";    // 自定义签名
            if (string.IsNullOrEmpty(tel) || !Regex.IsMatch(tel, "^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(14[0-9]{1})|(18[0-9]{1}))+\\d{8})$"))
            {
                return "";
            }
            switch (lib)
            {
                case SMSLib.SMSTemplateByReg: 
                    tempSmsCode = GlobalConfig.SMSTemplateByReg;
                    var code = (new Random()).Next(1000, 2000).ToString();
                    //SessionManager.SetSession("smsCode", code);
                    System.Web.HttpContext.Current.Cache.Insert("Cache:smsCode" + tel, code, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10));
                    smsContent = GlobalConfig.SMSTemplateByRegContent.Replace("#", "\"").Replace("${code}", code);  //10min
                    break;
                //case SMSLib.SMSTemplateByGet:
                //    tempSmsCode = GlobalConfig.SMSTemplateByGet;
                //    smsContent = GlobalConfig.SMSTemplateByGetContent.Replace("#", "\"").Replace("${customer}", param.GetString("customer"));
                //    break;
                //case SMSLib.SMSTemplateByArrived:
                //    tempSmsCode = GlobalConfig.SMSTemplateByArrived;
                //    smsContent = GlobalConfig.SMSTemplateByArrivedContent.Replace("#", "\"").Replace("${salesname}", param.GetString("salesname"));
                //    break;
                case SMSLib.SMSTemplateByOrders:
                    tempSmsCode = GlobalConfig.SMSTemplateByOrders;
                    smsContent = GlobalConfig.SMSTemplateByOrdersContent.Replace("#", "\"").Replace("${salesname}", param.GetString("salesname")).Replace("${customer}", param.GetString("customer")).Replace("${time}", param.GetString("time")).Replace("${sn}", param.GetString("sn"));
                    break;

                case SMSLib.SMSTemplateByHasReg: 
                    tempSmsCode = GlobalConfig.SMSTemplateByHasReg;
                    smsContent = GlobalConfig.SMSTemplateByHasRegContent.Replace("#", "\"").Replace("${adminname}", param.GetString("adminname")).Replace("${name}", param.GetString("name"));
                    break;

                //case SMSLib.SMSTemplateByPurchase:
                //    tempSmsCode = GlobalConfig.SMSTemplateByPurchase;  //customername  统一为customer
                //    smsContent = GlobalConfig.SMSTemplateByPurchaseContent.Replace("#", "\"").Replace("${customername}", param.GetString("customer"));
                //    break;

                case SMSLib.SMSTemplateByReview:
                    tempSmsCode = GlobalConfig.SMSTemplateByReview;  //customername  统一为customer
                    smsContent = GlobalConfig.SMSTemplateByReviewContent.Replace("#", "\"").Replace("${customername}", param.GetString("customer"));
                    break;

            }
            string url = GlobalConfig.SMSurl;
            string appkey = GlobalConfig.SMSappkey;
            string secret = GlobalConfig.SMSsecret;

            ITopClient client = new DefaultTopClient(url, appkey, secret);
            AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
            req.Extend = "123456";
            req.SmsType = "normal";
            req.SmsFreeSignName = smsSign;
            req.SmsParam = smsContent;
            req.RecNum = tel;
            req.SmsTemplateCode = tempSmsCode;
            AlibabaAliqinFcSmsNumSendResponse rsp = client.Execute(req);
            if (!rsp.Body.Contains("<success>true</success>"))  //发送不成功
                Logger.Warn(typeof(SMSManager), "method:SendSms," + rsp.Body);
            return rsp.Body;
        }
    }
    public enum SMSLib
    {
        /// <summary>
        /// 注册验证码模板   参数HashParam为： 验证码 code
        /// </summary>
        SMSTemplateByReg,
        ///// <summary>
        ///// 取货提醒模板  参数HashParam为： 客户 customer
        ///// </summary>
        //SMSTemplateByGet,
        ///// <summary>
        ///// 到货提醒模板  参数HashParam为：  业务 salesname
        ///// </summary>
        //SMSTemplateByArrived,
        /// <summary>
        /// 下单提醒模板  参数HashParam为： 客户 customer,业务 salesname,货号 sn ,下单时间time
        /// </summary>
        SMSTemplateByOrders,
        /// <summary>
        /// 已注册提醒模板  参数HashParam为： 管理员adminname 用户 name
        /// </summary>
        SMSTemplateByHasReg,
        /// <summary>
        /// 采购成功提醒模版   参数HashParam为： 客户 customer
        /// </summary>
        //SMSTemplateByPurchase,
        /// <summary>
        /// 审批通过成功提醒模板 客户 customername
        /// </summary>
        SMSTemplateByReview

    }

}
